const Router = require('koa-router')
const uuidv1 = require('uuid/v1')

const { userRegisterValidator } = require('../validators/userValidator')
const { User } = require('../model/user')
const { Auth } = require('../../middlewares/auth')

const router = new Router({
    prefix: '/api/v1'
})


/**
* @api {post} /api/v1/user/register 用户注册
* @apiGroup user
* @apiVersion 1.0.0
* @apiParam {String} username 参数
* @apiParam {String} password 参数
*
* @apiSuccess (success){String} validatorMsg 验证消息
* @apiSuccess (success){String} msg 返回消息
* @apiSuccess (success){Object} data 返回数据
* @apiSuccess (success){Int} errCode 状态码
* @apiSuccess (success){String} method 请求方式
* @apiSuccess (success){String} path 路径
* @apiSuccess (success){String} token 登录凭证
* @apiSuccess (success){String} username 用户名
*
* @apiSuccessExample {json} 成功:
* {
*   "validatorMsg": [],
*   "msg": "", // 成功信息
*   "data": {},
*   "errCode": 0, // 状态码
*   "method": "", // 请求方式
*   "path": "" // 路径
* }
*
* @apiError (error){Array} validatorMsg 验证消息
* @apiError (error){String} msg 返回消息
* @apiError (error){Null} data 返回数据
* @apiError (error){Int} errCode 状态码
* @apiError (error){String} method 请求方式
* @apiError (error){String} path 路径
*
* @apiErrorExample {json} 失败:
* {
*   "validatorMsg": [],
*   "msg": "", //错误信息
*   "data": null,
*   "errCode": 10000 //状态码,
*   "method": "", // 请求方式
*   "path": "" // 路径
* }
*/
router.post('/user/register', async (ctx, next) => {
    const v = await new userRegisterValidator().validate(ctx)
    
    const user = {
        username: v.get('body.username'),
        password: v.get('body.password'),
        uid: uuidv1()
    }
    const result = await User.checkuser(user)

    if (result) {
        throw new global.errs.notRestiger()
    }

    const successCreate = await User.create(user)

    if (!successCreate) {
        throw new global.errs.CreateAuthFailed()
    }

    const data = {
        token: Auth.generateToken(successCreate.uid),
        username: successCreate.username
    }

    throw new global.errs.Success(data, '注册成功')
})

/**
* @api {post} /api/v1/user/login 用户登录
* @apiGroup user
* @apiVersion 1.0.0
* @apiParam {String} username 参数
* @apiParam {String} password 参数
*
* @apiSuccess (success){String} validatorMsg 验证消息
* @apiSuccess (success){String} msg 返回消息
* @apiSuccess (success){Object} data 返回数据
* @apiSuccess (success){Int} errCode 状态码
* @apiSuccess (success){String} method 请求方式
* @apiSuccess (success){String} path 路径
* @apiSuccess (success){String} token 登录凭证
* @apiSuccess (success){String} username 用户名
*
* @apiSuccessExample {json} 成功:
* {
*   "validatorMsg": [],
*   "msg": "", // 成功信息
*   "data": {},
*   "errCode": 0, // 状态码
*   "method": "", // 请求方式
*   "path": "" // 路径
* }
*
* @apiError (error){Array} validatorMsg 验证消息
* @apiError (error){String} msg 返回消息
* @apiError (error){Null} data 返回数据
* @apiError (error){Int} errCode 状态码
* @apiError (error){String} method 请求方式
* @apiError (error){String} path 路径
*
* @apiErrorExample {json} 失败:
* {
*   "validatorMsg": [],
*   "msg": "", //错误信息
*   "data": null,
*   "errCode": 10000 //状态码,
*   "method": "", // 请求方式
*   "path": "" // 路径
* }
*/
router.post('/user/login', async (ctx, next) => {
    const v = await new userRegisterValidator().validate(ctx)
    
    const result = await User.verifyUserPassword(v.get('body.username'), v.get('body.password'))

    const data = {
        token: Auth.generateToken(result.uid),
        username: result.username
    }

    throw new global.errs.Success(data, '登录成功')
})

module.exports = router
